
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>附件2：async/await · Sample GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="yangbo">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-codeblock-filename/block.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-disqus/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../you-qing-zan-zhu.html" />
    
    
    <link rel="prev" href="fu-jian-1-settimeout-yu-bi-bao.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://www.chengweiyang.cn" target="_blank" class="custom-link">这波能反杀</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../ji-chu-jin-jie-xi-lie.html">
            
                <a href="../ji-chu-jin-jie-xi-lie.html">
            
                    
                    基础进阶系列
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="yi-3001-nei-cun-kong-jian-xiang-jie.html">
            
                <a href="yi-3001-nei-cun-kong-jian-xiang-jie.html">
            
                    
                    一、内存空间详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="er-3001-zhi-xing-shang-xia-wen.html">
            
                <a href="er-3001-zhi-xing-shang-xia-wen.html">
            
                    
                    二、执行上下文
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="san-3001-bian-liang-dui-xiang.html">
            
                <a href="san-3001-bian-liang-dui-xiang.html">
            
                    
                    三、变量对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="si-3001-zuo-yong-yu-lian-yu-bi-bao.html">
            
                <a href="si-3001-zuo-yong-yu-lian-yu-bi-bao.html">
            
                    
                    四、作用域链与闭包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="wu-3001-this.html">
            
                <a href="wu-3001-this.html">
            
                    
                    五、this
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="liu-3001-zai-chrome-zhong-guan-cha-han-shu-diao-yong-zhan-3001-zuo-yong-yu-lian-3001-bi-bao.html">
            
                <a href="liu-3001-zai-chrome-zhong-guan-cha-han-shu-diao-yong-zhan-3001-zuo-yong-yu-lian-3001-bi-bao.html">
            
                    
                    六、在chrome中观察函数调用栈、作用域链、闭包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="qi-3001-han-shu-yu-han-shu-shi-bian-cheng.html">
            
                <a href="qi-3001-han-shu-yu-han-shu-shi-bian-cheng.html">
            
                    
                    七、函数与函数式编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="ba-3001-ke-li-hua.html">
            
                <a href="ba-3001-ke-li-hua.html">
            
                    
                    八、柯里化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="jiu-3001-mian-xiang-dui-xiang.html">
            
                <a href="jiu-3001-mian-xiang-dui-xiang.html">
            
                    
                    九、面向对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10" data-path="shi-3001-mian-xiang-dui-xiang-shi-zhan-zhi-tuo-ye-dui-xiang-feng-zhuang.html">
            
                <a href="shi-3001-mian-xiang-dui-xiang-shi-zhan-zhi-tuo-ye-dui-xiang-feng-zhuang.html">
            
                    
                    十、面向对象实战之拖拽对象封装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.11" data-path="shi-yi-3001-qian-xi-jquery.html">
            
                <a href="shi-yi-3001-qian-xi-jquery.html">
            
                    
                    十一、浅析jQuery
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.12" data-path="shi-er-3001-shi-jian-xun-huan-ji-zhi.html">
            
                <a href="shi-er-3001-shi-jian-xun-huan-ji-zhi.html">
            
                    
                    十二、事件循环机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.13" data-path="shi-san-3001-promise.html">
            
                <a href="shi-san-3001-promise.html">
            
                    
                    十三、Promise
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.14" data-path="shi-si-3001-es6-chang-yong-zhi-shi-he-ji.html">
            
                <a href="shi-si-3001-es6-chang-yong-zhi-shi-he-ji.html">
            
                    
                    十四、ES6常用知识合集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.15" data-path="shi-wu-3001-xiang-jie-es6-modules.html">
            
                <a href="shi-wu-3001-xiang-jie-es6-modules.html">
            
                    
                    十五、详解ES6 Modules
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.16" data-path="fu-jian-1-settimeout-yu-bi-bao.html">
            
                <a href="fu-jian-1-settimeout-yu-bi-bao.html">
            
                    
                    附件1：setTimeout与闭包
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.17" data-path="fu-jian-2-async-await.html">
            
                <a href="fu-jian-2-async-await.html">
            
                    
                    附件2：async/await
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../you-qing-zan-zhu.html">
            
                <a href="../you-qing-zan-zhu.html">
            
                    
                    友情赞助
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >附件2：async/await</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>&#x5728;&#x5B9E;&#x9645;&#x5F00;&#x53D1;&#x4E2D;&#x603B;&#x4F1A;&#x9047;&#x5230;&#x8BB8;&#x591A;&#x5F02;&#x6B65;&#x7684;&#x95EE;&#x9898;&#xFF0C;&#x6700;&#x5E38;&#x89C1;&#x7684;&#x573A;&#x666F;&#x4FBF;&#x662F;&#x63A5;&#x53E3;&#x8BF7;&#x6C42;&#x4E4B;&#x540E;&#x4E00;&#x5B9A;&#x8981;&#x7B49;&#x4E00;&#x6BB5;&#x65F6;&#x95F4;&#x624D;&#x80FD;&#x5F97;&#x5230;&#x7ED3;&#x679C;&#xFF0C;&#x5982;&#x679C;&#x9047;&#x5230;&#x591A;&#x4E2A;&#x63A5;&#x53E3;&#x524D;&#x540E;&#x4F9D;&#x8D56;&#xFF0C;&#x90A3;&#x4E48;&#x95EE;&#x9898;&#x5C31;&#x53D8;&#x5F97;&#x590D;&#x6742;&#x3002;&#x5927;&#x5BB6;&#x90FD;&#x4E00;&#x76F4;&#x5728;&#x5C1D;&#x8BD5;&#x4F7F;&#x7528;&#x66F4;&#x597D;&#x7684;&#x65B9;&#x6848;&#x6765;&#x89E3;&#x51B3;&#x8FD9;&#x4E9B;&#x95EE;&#x9898;&#x3002;&#x6700;&#x5F00;&#x59CB;&#x53EA;&#x80FD;&#x5229;&#x7528;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x540E;&#x6765;&#x5F00;&#x59CB;&#x6709;&#x4EBA;&#x4F7F;&#x7528;Promise&#x7684;&#x601D;&#x7EF4;&#x6765;&#x641E;&#x5B9A;&#x3002;&#x5230;ES6&#x4E2D;&#x5F00;&#x59CB;&#x652F;&#x6301;&#x539F;&#x751F;&#x7684;Promise&#xFF0C;&#x5F15;&#x5165;Generator&#x51FD;&#x6570;&#x3002;</p>
<p>&#x76F4;&#x5230;ES7&#xFF0C;&#x6709;&#x4E86;<code>async/await</code>&#x3002;</p>
<p>&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x7528;&#x540C;&#x6B65;&#x7684;&#x601D;&#x7EF4;&#x6765;&#x89E3;&#x51B3;&#x5F02;&#x6B65;&#x95EE;&#x9898;&#x7684;&#x65B9;&#x6848;&#x3002;</p>
<p>&#x6211;&#x60F3;&#x5F88;&#x591A;&#x4EBA;&#x53EF;&#x80FD;&#x8FD8;&#x4E0D;&#x592A;&#x5206;&#x5F97;&#x6E05;&#x540C;&#x6B65;&#x4E0E;&#x5F02;&#x6B65;&#x7684;&#x533A;&#x522B;&#x3002;&#x5982;&#x679C;&#x4F60;&#x5DF2;&#x7ECF;&#x5F7B;&#x5E95;&#x4E86;&#x89E3;&#x4E86;<a href="http://www.jianshu.com/p/12b9f73c5a4f" target="_blank">&#x4E8B;&#x4EF6;&#x5FAA;&#x73AF;</a>&#xFF0C;&#x90A3;&#x4E48;&#x60F3;&#x5FC5;&#x5BF9;&#x5F02;&#x6B65;&#x7684;&#x6982;&#x5FF5;&#x5E94;&#x8BE5;&#x975E;&#x5E38;&#x4E86;&#x89E3;&#x3002;&#x5F53;&#x6211;&#x4EEC;&#x53D1;&#x51FA;&#x4E86;&#x8BF7;&#x6C42;&#xFF0C;&#x5E76;&#x4E0D;&#x4F1A;&#x7B49;&#x5F85;&#x54CD;&#x5E94;&#x7ED3;&#x679C;&#xFF0C;&#x800C;&#x662F;&#x4F1A;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#x540E;&#x9762;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x54CD;&#x5E94;&#x7ED3;&#x679C;&#x7684;&#x5904;&#x7406;&#x5728;&#x4E4B;&#x540E;&#x7684;&#x4E8B;&#x4EF6;&#x5FAA;&#x73AF;&#x4E2D;&#x89E3;&#x51B3;&#x3002;&#x90A3;&#x4E48;&#x540C;&#x6B65;&#x7684;&#x610F;&#x601D;&#xFF0C;&#x5C31;&#x662F;&#x7B49;&#x7ED3;&#x679C;&#x51FA;&#x6765;&#x4E4B;&#x540E;&#xFF0C;&#x4EE3;&#x7801;&#x624D;&#x4F1A;&#x7EE7;&#x7EED;&#x5F80;&#x4E0B;&#x6267;&#x884C;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x7528;&#x4E00;&#x4E2A;&#x4E24;&#x4EBA;&#x95EE;&#x7B54;&#x7684;&#x573A;&#x666F;&#x6765;&#x6BD4;&#x55BB;&#x5F02;&#x6B65;&#x4E0E;&#x540C;&#x6B65;&#x3002;A&#x5411;B&#x95EE;&#x4E86;&#x4E00;&#x4E2A;&#x95EE;&#x9898;&#x4E4B;&#x540E;&#xFF0C;&#x4E0D;&#x7B49;&#x5F85;B&#x7684;&#x56DE;&#x7B54;&#xFF0C;&#x63A5;&#x7740;&#x95EE;&#x4E0B;&#x4E00;&#x4E2A;&#x95EE;&#x9898;&#xFF0C;&#x8FD9;&#x662F;&#x5F02;&#x6B65;&#x3002;A&#x5411;B&#x95EE;&#x4E86;&#x4E00;&#x4E2A;&#x95EE;&#x9898;&#x4E4B;&#x540E;&#xFF0C;&#x7136;&#x540E;&#x5C31;&#x7B11;&#x5475;&#x5475;&#x7684;&#x7B49;&#x7740;B&#x56DE;&#x7B54;&#xFF0C;B&#x56DE;&#x7B54;&#x4E86;&#x4E4B;&#x540E;&#x4ED6;&#x624D;&#x4F1A;&#x63A5;&#x7740;&#x95EE;&#x4E0B;&#x4E00;&#x4E2A;&#x95EE;&#x9898;&#xFF0C;&#x8FD9;&#x662F;&#x540C;&#x6B65;&#x3002;</p>
<p>&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x5148;&#x8BB0;&#x4F4F;&#x8FD9;&#x4E2A;&#x7279;&#x70B9;&#xFF0C;<code>async/await</code>&#x4F7F;&#x7528;&#x540C;&#x6B65;&#x7684;&#x601D;&#x7EF4;&#xFF0C;&#x6765;&#x89E3;&#x51B3;&#x5F02;&#x6B65;&#x7684;&#x95EE;&#x9898;&#x3002;</p>
<p>&#x5728;&#x7EE7;&#x7EED;&#x5206;&#x6790;&#x5B83;&#x7684;&#x8BED;&#x6CD5;&#x4E0E;&#x4F7F;&#x7528;&#x4E4B;&#x524D;&#xFF0C;&#x6211;&#x4EEC;&#x5148;&#x4ECB;&#x7ECD;&#x4E00;&#x4E0B;&#x5982;&#x4F55;&#x5728;&#x6211;&#x4EEC;&#x7684;&#x5F00;&#x53D1;&#x73AF;&#x5883;&#x4E2D;&#x652F;&#x6301;&#x8BE5;&#x8BED;&#x6CD5;&#x3002;</p>
<blockquote>
<p>&#x5982;&#x679C;&#x4F60;&#x5DF2;&#x7ECF;&#x77E5;&#x9053;&#x5982;&#x4F55;&#x914D;&#x7F6E;&#xFF0C;&#x53EF;&#x8DF3;&#x8FC7;</p>
</blockquote>
<h3 id="&#x4E00;&#x3001;&#x5982;&#x4F55;&#x5728;&#x81EA;&#x5DF1;&#x7684;&#x5F00;&#x53D1;&#x73AF;&#x5883;&#x4E2D;&#x652F;&#x6301;asyncawait&#x8BED;&#x6CD5;">&#x4E00;&#x3001;&#x5982;&#x4F55;&#x5728;&#x81EA;&#x5DF1;&#x7684;&#x5F00;&#x53D1;&#x73AF;&#x5883;&#x4E2D;&#x652F;&#x6301;<code>async/await</code>&#x8BED;&#x6CD5;</h3>
<p>&#x8FD9;&#x91CC;&#x4E3B;&#x8981;&#x4ECB;&#x7ECD;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#x3002;</p>
<h4 id="1-webpack&#x4E2D;&#x652F;&#x6301;&#x8BE5;&#x8BED;&#x6CD5;">1. webpack&#x4E2D;&#x652F;&#x6301;&#x8BE5;&#x8BED;&#x6CD5;</h4>
<p>&#x9996;&#x5148;&#x5728;&#x5F53;&#x524D;&#x9879;&#x76EE;&#x4E2D;&#x4F7F;&#x7528;npm&#x4E0B;&#x8F7D;<code>babel-loader</code>&#x3002;</p>
<pre><code class="lang-javascript">&gt; npm install babel-loader --save-dev
</code></pre>
<p>&#x7136;&#x540E;&#x5728;&#x914D;&#x7F6E;&#x6587;&#x4EF6;<code>webpack.confing.dev.js</code>&#x4E2D;&#x914D;&#x7F6E;&#xFF0C;&#x5728;<code>module.exports.module.rules</code>&#x4E2D;&#x6DFB;&#x52A0;&#x5982;&#x4E0B;&#x914D;&#x7F6E;&#x5143;&#x7D20;&#x5373;&#x53EF;&#x3002;</p>
<pre><code class="lang-javascript">  {
    test: <span class="hljs-regexp">/\.(js|jsx)$/</span>,
    include: paths.appSrc,
    loader: <span class="hljs-built_in">require</span>.resolve(<span class="hljs-string">&apos;babel-loader&apos;</span>),
    options: {
      cacheDirectory: <span class="hljs-literal">true</span>,
    },
  },
</code></pre>
<blockquote>
<p>&#x5982;&#x679C;&#x4F60;&#x4F7F;&#x7528;&#x6700;&#x65B0;&#x7248;&#x672C;&#x7684;create-react-app&#x6216;&#x8005;vue-cli&#x6765;&#x6784;&#x5EFA;&#x4F60;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x90A3;&#x4E48;&#x5B83;&#x4EEC;&#x5E94;&#x8BE5;&#x5DF2;&#x7ECF;&#x652F;&#x6301;&#x4E86;&#x8BE5;&#x914D;&#x7F6E;&#x3002;</p>
</blockquote>
<h5 id="2-gulp&#x4E2D;&#x652F;&#x6301;&#x8BE5;&#x8BED;&#x6CD5;">2. gulp&#x4E2D;&#x652F;&#x6301;&#x8BE5;&#x8BED;&#x6CD5;</h5>
<p>&#x9996;&#x5148;&#x5B89;&#x88C5;gulp&#x63D2;&#x4EF6;</p>
<pre><code class="lang-javascript">&gt; npm install gulp-babel --save-dev
</code></pre>
<p>&#x7136;&#x540E;&#x7F16;&#x5199;&#x4EFB;&#x52A1;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> gulp = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;gulp&apos;</span>);
<span class="hljs-keyword">var</span> babel = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;gulp-babel&apos;</span>);

gulp.task(<span class="hljs-string">&apos;babel&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> gulp.src(<span class="hljs-string">&apos;src/app.js&apos;</span>)
    .pipe(babel())
    .pipe(gulp.dest(<span class="hljs-string">&apos;dist&apos;</span>));
});
</code></pre>
<h4 id="&#x4E8C;&#x3001;&#x5982;&#x4F55;&#x4F7F;&#x7528;">&#x4E8C;&#x3001;&#x5982;&#x4F55;&#x4F7F;&#x7528;</h4>
<p>async&#x51FD;&#x6570;&#x662F;Generator&#x7684;&#x4E00;&#x4E2A;&#x8BED;&#x6CD5;&#x7CD6;&#x3002;&#x5982;&#x679C;&#x4F60;&#x4E0D;&#x77E5;&#x9053;Generator&#x662F;&#x4EC0;&#x4E48;&#x51FD;&#x6570;&#x4E5F;&#x6CA1;&#x6709;&#x5173;&#x7CFB;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x77E5;&#x9053;async&#x51FD;&#x6570;&#x5B9E;&#x9645;&#x4E0A;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x4E00;&#x4E2A;Promise&#x5BF9;&#x8C61;&#x5373;&#x53EF;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fn</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-number">30</span>;
}

<span class="hljs-comment">// &#x6216;&#x8005;</span>
<span class="hljs-keyword">const</span> fn = <span class="hljs-keyword">async</span> () =&gt; {
    <span class="hljs-keyword">return</span> <span class="hljs-number">30</span>;
}
</code></pre>
<p>&#x5728;&#x58F0;&#x660E;&#x51FD;&#x6570;&#x65F6;&#xFF0C;&#x524D;&#x9762;&#x52A0;&#x4E0A;&#x5173;&#x952E;&#x5B57;<code>async</code>&#xFF0C;&#x8FD9;&#x5C31;&#x662F;<code>async</code>&#x7684;&#x7528;&#x6CD5;&#x3002;&#x5F53;&#x6211;&#x4EEC;&#x7528;<code>console.log</code>&#x6253;&#x5370;&#x51FA;&#x4E0A;&#x9762;&#x58F0;&#x660E;&#x7684;&#x51FD;&#x6570;fn&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x5982;&#x4E0B;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(fn());

<span class="hljs-comment">// result</span>
<span class="hljs-built_in">Promise</span> = {
    __proto__: <span class="hljs-built_in">Promise</span>,
    [[PromiseStatus]]: <span class="hljs-string">&quot;resolved&quot;</span>,
    [[PromiseValue]]: <span class="hljs-number">30</span>
}
</code></pre>
<p>&#x5F88;&#x663E;&#x7136;&#xFF0C;fn&#x7684;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x5176;&#x5B9E;&#x5C31;&#x662F;&#x4E00;&#x4E2A;Promise&#x5BF9;&#x8C61;&#x3002;&#x56E0;&#x6B64;&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;then&#x6765;&#x5904;&#x7406;&#x540E;&#x7EED;&#x903B;&#x8F91;&#x3002;</p>
<pre><code class="lang-javascript">fn().then(res =&gt; {
    <span class="hljs-built_in">console</span>.log(res);  <span class="hljs-comment">// 30</span>
})
</code></pre>
<p>await&#x7684;&#x542B;&#x4E49;&#x4E3A;&#x7B49;&#x5F85;&#x3002;&#x610F;&#x601D;&#x5C31;&#x662F;&#x4EE3;&#x7801;&#x9700;&#x8981;&#x7B49;&#x5F85;await&#x540E;&#x9762;&#x7684;&#x51FD;&#x6570;&#x8FD0;&#x884C;&#x5B8C;&#x5E76;&#x4E14;&#x6709;&#x4E86;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x4E4B;&#x540E;&#xFF0C;&#x624D;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x3002;&#x8FD9;&#x6B63;&#x662F;&#x540C;&#x6B65;&#x7684;&#x6548;&#x679C;&#x3002;</p>
<p>&#x4F46;&#x662F;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;await&#x5173;&#x952E;&#x5B57;&#x53EA;&#x80FD;&#x5728;async&#x51FD;&#x6570;&#x4E2D;&#x4F7F;&#x7528;&#x3002;&#x5E76;&#x4E14;await&#x540E;&#x9762;&#x7684;&#x51FD;&#x6570;&#x8FD0;&#x884C;&#x540E;&#x5FC5;&#x987B;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;Promise&#x5BF9;&#x8C61;&#x624D;&#x80FD;&#x5B9E;&#x73B0;&#x540C;&#x6B65;&#x7684;&#x6548;&#x679C;&#x3002;</p>
<p>&#x5F53;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x53BB;&#x63A5;&#x6536;await&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x65F6;&#xFF0C;&#x8BE5;&#x8FD4;&#x56DE;&#x503C;&#x4E3A;Promise&#x4E2D;resolve&#x51FA;&#x6765;&#x7684;&#x503C;&#xFF08;&#x4E5F;&#x5C31;&#x662F;PromiseValue&#xFF09;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x8FD4;&#x56DE;Promise&#x5BF9;&#x8C61;&#x7684;&#x51FD;&#x6570;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fn</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>((resolve, reject) =&gt; {
        setTimeout(() =&gt; {
            resolve(<span class="hljs-number">30</span>);
        }, <span class="hljs-number">1000</span>);
    })
}

<span class="hljs-comment">// &#x7136;&#x540E;&#x5229;&#x7528;async/await&#x6765;&#x5B8C;&#x6210;&#x4EE3;&#x7801;</span>
<span class="hljs-keyword">const</span> foo = <span class="hljs-keyword">async</span> () =&gt; {
    <span class="hljs-keyword">const</span> t = <span class="hljs-keyword">await</span> fn();
    <span class="hljs-built_in">console</span>.log(t);
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;next code&apos;</span>);
}

foo();

<span class="hljs-comment">// result:</span>
<span class="hljs-comment">// 30</span>
<span class="hljs-comment">// next code</span>
</code></pre>
<p>&#x8FD0;&#x884C;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#xFF0C;&#x5F53;&#x5728;async&#x51FD;&#x6570;&#x4E2D;&#xFF0C;&#x8FD0;&#x884C;&#x9047;&#x5230;await&#x65F6;&#xFF0C;&#x5C31;&#x4F1A;&#x7B49;&#x5F85;await&#x540E;&#x9762;&#x7684;&#x51FD;&#x6570;&#x8FD0;&#x884C;&#x5B8C;&#x6BD5;&#xFF0C;&#x800C;&#x4E0D;&#x4F1A;&#x76F4;&#x63A5;&#x6267;&#x884C;<code>next code</code>&#x3002;</p>
<p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x76F4;&#x63A5;&#x4F7F;&#x7528;then&#x65B9;&#x6CD5;&#x7684;&#x8BDD;&#xFF0C;&#x60F3;&#x8981;&#x8FBE;&#x5230;&#x540C;&#x6837;&#x7684;&#x7ED3;&#x679C;&#xFF0C;&#x5C31;&#x4E0D;&#x5F97;&#x4E0D;&#x628A;&#x540E;&#x7EED;&#x7684;&#x903B;&#x8F91;&#x5199;&#x5728;then&#x65B9;&#x6CD5;&#x4E2D;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> foo = () =&gt; {
    <span class="hljs-keyword">return</span> fn().then(t =&gt; {
        <span class="hljs-built_in">console</span>.log(t);
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;next code&apos;</span>);    
    })
}

foo();
</code></pre>
<p>&#x5F88;&#x663E;&#x7136;&#x5982;&#x679C;&#x4F7F;&#x7528;async/await&#x7684;&#x8BDD;&#xFF0C;&#x4EE3;&#x7801;&#x7ED3;&#x6784;&#x4F1A;&#x66F4;&#x52A0;&#x7B80;&#x6D01;&#xFF0C;&#x903B;&#x8F91;&#x4E5F;&#x66F4;&#x52A0;&#x6E05;&#x6670;&#x3002;</p>
<h6 id="&#x5F02;&#x5E38;&#x5904;&#x7406;">&#x5F02;&#x5E38;&#x5904;&#x7406;</h6>
<p>&#x5728;Promise&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x77E5;&#x9053;&#x662F;&#x901A;&#x8FC7;catch&#x7684;&#x65B9;&#x5F0F;&#x6765;&#x6355;&#x83B7;&#x5F02;&#x5E38;&#x3002;&#x800C;&#x5F53;&#x6211;&#x4EEC;&#x4F7F;&#x7528;async&#x65F6;&#xFF0C;&#x5219;&#x901A;&#x8FC7;<code>try/catch</code>&#x6765;&#x6355;&#x83B7;&#x5F02;&#x5E38;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fn</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>((resolve, reject) =&gt; {
        setTimeout(() =&gt; {
            reject(<span class="hljs-string">&apos;some error.&apos;</span>);
        }, <span class="hljs-number">1000</span>);
    })
}

<span class="hljs-keyword">const</span> foo = <span class="hljs-keyword">async</span> () =&gt; {
    <span class="hljs-keyword">try</span> {
        <span class="hljs-keyword">await</span> fn();
    } <span class="hljs-keyword">catch</span> (e) {
        <span class="hljs-built_in">console</span>.log(e);  <span class="hljs-comment">// some error</span>
    }
}

foo();
</code></pre>
<p>&#x5982;&#x679C;&#x6709;&#x591A;&#x4E2A;await&#x51FD;&#x6570;&#xFF0C;&#x90A3;&#x4E48;&#x53EA;&#x4F1A;&#x8FD4;&#x56DE;&#x7B2C;&#x4E00;&#x4E2A;&#x6355;&#x83B7;&#x5230;&#x7684;&#x5F02;&#x5E38;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fn1</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>((resolve, reject) =&gt; {
        setTimeout(() =&gt; {
            reject(<span class="hljs-string">&apos;some error fn1.&apos;</span>);
        }, <span class="hljs-number">1000</span>);
    })
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fn2</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>((resolve, reject) =&gt; {
        setTimeout(() =&gt; {
            reject(<span class="hljs-string">&apos;some error fn2.&apos;</span>);
        }, <span class="hljs-number">1000</span>);
    })
}

<span class="hljs-keyword">const</span> foo = <span class="hljs-keyword">async</span> () =&gt; {
    <span class="hljs-keyword">try</span> {
        <span class="hljs-keyword">await</span> fn1();
        <span class="hljs-keyword">await</span> fn2();
    } <span class="hljs-keyword">catch</span> (e) {
        <span class="hljs-built_in">console</span>.log(e);  <span class="hljs-comment">// some error fn1.</span>
    }
}

foo();
</code></pre>
<h6 id="&#x5B9E;&#x8DF5;">&#x5B9E;&#x8DF5;</h6>
<p>&#x5728;&#x5B9E;&#x8DF5;&#x4E2D;&#x6211;&#x4EEC;&#x9047;&#x5230;&#x5F02;&#x6B65;&#x573A;&#x666F;&#x6700;&#x591A;&#x7684;&#x5C31;&#x662F;&#x63A5;&#x53E3;&#x8BF7;&#x6C42;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x91CC;&#x5C31;&#x4EE5;jquery&#x4E2D;&#x7684;<code>$.get</code>&#x4E3A;&#x4F8B;&#x7B80;&#x5355;&#x5C55;&#x793A;&#x4E00;&#x4E0B;&#x5982;&#x4F55;&#x914D;&#x5408;<code>async/await</code>&#x6765;&#x89E3;&#x51B3;&#x8FD9;&#x4E2A;&#x573A;&#x666F;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x5148;&#x5B9A;&#x4E49;&#x63A5;&#x53E3;&#x8BF7;&#x6C42;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x7531;&#x4E8E;jquery&#x5C01;&#x88C5;&#x7684;&#x51E0;&#x4E2A;&#x8BF7;&#x6C42;&#x65B9;&#x6CD5;&#x90FD;&#x662F;&#x8FD4;&#x56DE;Promise&#x5B9E;&#x4F8B;&#xFF0C;&#x56E0;&#x6B64;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;await&#x51FD;&#x6570;&#x5B9E;&#x73B0;&#x540C;&#x6B65;</span>
<span class="hljs-keyword">const</span> getUserInfo = () =&gt; $.get(<span class="hljs-string">&apos;xxxx/api/xx&apos;</span>);

<span class="hljs-keyword">const</span> clickHandler = <span class="hljs-keyword">async</span> () =&gt; {
    <span class="hljs-keyword">try</span> {
        <span class="hljs-keyword">const</span> resp = <span class="hljs-keyword">await</span> getUserInfo();
        <span class="hljs-comment">// resp&#x4E3A;&#x63A5;&#x53E3;&#x8FD4;&#x56DE;&#x5185;&#x5BB9;&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x5229;&#x7528;&#x5B83;&#x6765;&#x5904;&#x7406;&#x5BF9;&#x5E94;&#x7684;&#x903B;&#x8F91;</span>
        <span class="hljs-built_in">console</span>.log(resp);

        <span class="hljs-comment">// do something</span>
    } <span class="hljs-keyword">catch</span> (e) {
        <span class="hljs-comment">// &#x5904;&#x7406;&#x9519;&#x8BEF;&#x903B;&#x8F91;</span>
    }
}
</code></pre>
<blockquote>
<p>&#x4E3A;&#x4E86;&#x4FDD;&#x8BC1;&#x903B;&#x8F91;&#x7684;&#x5B8C;&#x6574;&#x6027;&#xFF0C;&#x5728;&#x5B9E;&#x8DF5;&#x4E2D;<code>try/catch</code>&#x5FC5;&#x4E0D;&#x53EF;&#x5C11;&#x3002;&#x603B;&#x4E4B;&#xFF0C;&#x4E0D;&#x5904;&#x7406;&#x9519;&#x8BEF;&#x903B;&#x8F91;&#x7684;&#x7A0B;&#x5E8F;&#x5458;&#x4E0D;&#x662F;&#x597D;&#x7A0B;&#x5E8F;&#x5458;&#x3002;</p>
</blockquote>
<p>&#x4E0E;Promise&#x76F8;&#x6BD4;&#xFF0C;&#x4E2A;&#x4EBA;&#x8BA4;&#x4E3A;<code>async/await</code>&#x6709;&#x4E00;&#x5B9A;&#x7684;&#x7B80;&#x6D01;&#x6027;&#xFF0C;&#x4F46;&#x4E5F;&#x5E76;&#x975E;&#x5C31;&#x6BD4;Promise&#x6709;&#x7EDD;&#x5BF9;&#x7684;&#x4F18;&#x52BF;&#xFF0C;&#x56E0;&#x6B64;&#x53EA;&#x80FD;&#x7B97;&#x662F;&#x63D0;&#x4F9B;&#x4E86;&#x53E6;&#x5916;&#x4E00;&#x79CD;&#x540C;&#x6837;&#x5F88;&#x68D2;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x81F3;&#x4E8E;&#x5927;&#x5BB6;&#x5B66;&#x4E60;&#x4E4B;&#x540E;&#x9009;&#x62E9;&#x54EA;&#x79CD;&#x65B9;&#x5F0F;&#x6765;&#x89E3;&#x51B3;&#x81EA;&#x5DF1;&#x7684;&#x95EE;&#x9898;&#xFF0C;&#x6211;&#x8BA4;&#x4E3A;&#x8FD9;&#x4EC5;&#x4EC5;&#x53EA;&#x662F;&#x4E2A;&#x4EBA;&#x7684;&#x559C;&#x597D;&#x95EE;&#x9898;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="fu-jian-1-settimeout-yu-bi-bao.html" class="navigation navigation-prev " aria-label="Previous page: 附件1：setTimeout与闭包">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../you-qing-zan-zhu.html" class="navigation navigation-next " aria-label="Next page: 友情赞助">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"附件2：async/await","level":"1.2.17","depth":2,"next":{"title":"友情赞助","level":"1.3","depth":1,"path":"you-qing-zan-zhu.md","ref":"you-qing-zan-zhu.md","articles":[]},"previous":{"title":"附件1：setTimeout与闭包","level":"1.2.16","depth":2,"path":"ji-chu-jin-jie-xi-lie/fu-jian-1-settimeout-yu-bi-bao.md","ref":"ji-chu-jin-jie-xi-lie/fu-jian-1-settimeout-yu-bi-bao.md","articles":[]},"dir":"ltr"},"config":{"plugins":["codeblock-filename","disqus"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"disqus":{"useIdentifier":false,"shortName":"yangbo5207"},"codeblock-filename":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"yangbo","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Sample GitBook","links":{"sharing":{"all":null,"facebook":null,"google":null,"twitter":null,"weibo":null},"sidebar":{"这波能反杀":"http://www.chengweiyang.cn"}},"gitbook":"*","description":"This is a sample book created by gitbook","extension":null},"file":{"path":"ji-chu-jin-jie-xi-lie/fu-jian-2-async-await.md","mtime":"2017-12-01T05:24:16.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2017-12-01T11:38:51.460Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/URI.js/1.16.1/URI.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-disqus/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

